﻿using eLoan.Models;
using System;
using System.IO;
using System.Web.Mvc;

namespace eLoan.Controllers
{
    public class BaseController: Controller
    {
        const string error = "You are not authorized to view this page.";

        public string ServerReportsUrlPath
        {
            get
            {
                return "http://localhost:8083/";
            }
        }

        public string ClientReportsPath
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\";
            }
        }

        public string ServerBranchReportsPath
        {
            get
            {
                //return Request.PhysicalApplicationPath + @"Reports\";
                return @"c:\" + AdministrationHelper.GetLoggedInUserBranchCode(new DatabaseContext(), User.Identity.Name) + @"_Reports\";
            }
        }

        public string SelectedBranchReportsPath(string branchCode)
        {
            return @"c:\" + branchCode + @"_Reports\";
        }

        public string ServerAdminReportsPath
        {
            get
            {
                //return Request.PhysicalApplicationPath + @"Reports\";
                return @"c:\ADMIN_Reports\";
            }
        }

        public void CheckUserPageAccess(AdministrationHelper.UserPageAccess page)
        { 
            using (var db = new DatabaseContext())
            {
                var access = AdministrationHelper.GetUserAccess(db, User.Identity.Name);
                var role = AdministrationHelper.GetLoggedInUserRoleID(db, User.Identity.Name);
                switch (page)
                {
                    #region Administration access
                    case AdministrationHelper.UserPageAccess.Users:
                        {
                            if (!access.HasUsersAccess )
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.Banks:
                        {
                            if (!access.HasBanksAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.Branches:
                        {
                            if (!access.HasBranchesAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.Agents:
                        {
                            if (!access.HasAgentsAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.LoanTypes:
                        {
                            if (!access.HasLoanTypesAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ChargeTypes:
                        {
                            if (!access.HasChargeTypesAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.PaymentTypes:
                        {
                            if (!access.HasPaymentTypesAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.Notifications:
                        {
                            if (!access.HasNotificationsAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Masterfile access

                    case AdministrationHelper.UserPageAccess.AddressBook:
                        {
                            if (!access.HasAddressBookAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.Prospects:
                        {
                            if (!access.HasAddressBookAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Funding acess

                    case AdministrationHelper.UserPageAccess.Loans:
                        {
                            if (!access.HasLoansAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.CancelLoan:
                        {
                            if (!access.HasCancelLoanAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.PrintCheque:
                        {
                            if (!access.HasPrintChequeAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.PrintContract:
                        {
                            if (!access.HasPrintContractAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ReleaseToBranch:
                        {
                            if (!access.HasReleaseToBranchAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ReleaseToCustomer:
                        {
                            if (!access.HasReleaseToCustomerAccess)
                            { throw new Exception(error); }
                            break;
                        }
                    #endregion

                    #region Workspace

                    case AdministrationHelper.UserPageAccess.LoanPayment:
                        {
                            if (!access.HasLoanPaymentAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ChequeClearing:
                        {
                            if (!access.HasChequeClearingAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.LoanPenalty:
                        {
                            if (!access.HasLoanPenaltyAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.VoidLoanPayment:
                        {
                            if (!access.HasVoidLoanPaymentAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.RequestCloseLoan:
                        {
                            if (!access.HasRequestCloseLoanAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.CloseLoan:
                        {
                            if (!access.HasCloseLoanAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.LoanNotification:
                        {
                            if (!access.HasLoanNotificationAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.DailyCollectionReconciliation:
                        {
                            if (!access.HasDailyCollectionReconciliationAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    #endregion

                    #region Reports access

                    case AdministrationHelper.UserPageAccess.AgentItinerary:
                        {
                            if (!access.HasAgentItineraryAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.CollectionReport:
                        {
                            if (!access.HasCollectionReportAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.SalesReport:
                        {
                            if (!access.HasSalesReportAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ChequeReport:
                        {
                            if (!access.HasChequeReportAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.SavingsReport:
                        {
                            if (!access.HasSavingsReportAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.StatementOfAccount:
                        {
                            if (!access.HasSOAAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.PenaltyHistory:
                        {
                            if (!access.HasPenaltyHistoryAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.ClosedLoansReport:
                        {
                            if (!access.HasClosedLoansReportAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.DuePayments:
                        {
                            if (!access.HasDuePaymentsAccess)
                            { throw new Exception(error); }
                            break;
                        }

                    case AdministrationHelper.UserPageAccess.DailyCollectionReconciliationReport:
                        {
                            if (!access.HasDailyCollectionReconciliationReportAccess)
                            { throw new Exception(error); }
                            break;
                        }
                    case AdministrationHelper.UserPageAccess.AgingReport:
                        {
                            if (!access.HasAgingReportAccess)
                            { throw new Exception(error); }
                            break;
                        }
                        #endregion
                }
            }
            
        }

        protected void DownloadFile(string fileName, byte[] data)
        {
            using (Stream st = new MemoryStream(data))
            {
                long dataLengthToRead = st.Length;
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", @"attachment; filename=" + fileName);
                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    Int32 lengthRead = st.Read(data, 0, data.Length);
                    Response.OutputStream.Write(data, 0, lengthRead);
                    Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }

                Response.Flush();
                Response.Close();
            }

            Response.End();
        }
    }
}